package com.ikas.ai.server.module.machine.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.aop.Pageable;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.machine.model.dto.MachineSystemAddDTO;
import com.ikas.ai.server.module.machine.model.dto.MachineSystemUpdateDTO;
import com.ikas.ai.server.module.machine.service.MachineSystemService;
import com.ikas.ai.server.module.system.model.LogOpsType;
import com.ikas.ai.utils.NumUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 机组系统管理
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/2/1 10:49
 */
@RestController
@RequestMapping("/machine/system")
public class MachineSystemController extends AbstractBean {

    @Resource
    private MachineSystemService machineSystemService;

    /**
     * 获取设备资产管理分页数据
     *
     * @param machineNo
     * @param machineNo
     * @param systemNo
     * @return json
     */
    @Pageable(sortType = "desc")
    @GetMapping("/page")
    public JsonResult pageQuerySystem(@RequestParam(value = "machineNo", required = false) String machineNo,
                                      @RequestParam(value = "systemName", required = false) String systemName,
                                      @RequestParam(value = "systemNo", required = false) String systemNo) {
        return JsonResult.ok(machineSystemService.pageQuerySystem(machineNo, systemName, systemNo));
    }

    /**
     * 机组系统下拉框
     *
     * @return list
     */
    @GetMapping("/list")
    public JsonResult getSystemList(@RequestParam(value = "machineNo", required = false) String machineNo,
                                    @RequestParam(value = "systemName", required = false) String systemName,
                                    @RequestParam(value = "systemNo", required = false) String systemNo) {
        return JsonResult.ok(machineSystemService.getSystemList(machineNo, systemName, systemNo));
    }

    /**
     * 机组下拉框
     *
     * @return
     */
    @GetMapping("/list/machine")
    public JsonResult getMachineList(@RequestParam(name = "type", required = false) Integer type) {
        return JsonResult.ok(machineSystemService.getMachineList(type));
    }

    /**
     * 新增
     *
     * @param machineSystemAddDTO
     * @return JsonResult
     */
    @PostMapping("/add")
    public JsonResult addMachineSystem(@RequestBody @Validated MachineSystemAddDTO machineSystemAddDTO) {
        return JsonResult.ok(machineSystemService.addMachineSystem(machineSystemAddDTO, getUserId()));
    }

    /**
     * 编辑
     *
     * @param machineSystemUpdateDTO
     * @return
     */
    @PostMapping("/update")
    public JsonResult updateMachineSystem(@RequestBody @Validated MachineSystemUpdateDTO machineSystemUpdateDTO) {
        return JsonResult.ok(machineSystemService.updateMachineSystem(machineSystemUpdateDTO, getUserId()));
    }

    /**
     * 删除机组系统
     *
     * @param request
     * @param ids
     * @return
     */
    @PostMapping("/{ids}")
    public JsonResult deleteMachineSystem(HttpServletRequest request, @PathVariable(name = "ids") String ids) {
        List<Long> machineSystemIds = new ArrayList<>();
        for (String id : ids.split(",")) {
            if (!NumUtil.isLong(id, 0, Long.MAX_VALUE)) {
                continue;
            }
            machineSystemIds.add(Long.parseLong(id));
        }
        int row = machineSystemService.deleteMachineSystem(machineSystemIds);
        this.logOperation(request, LogOpsType.DICT_MANAGE, String.format("删除机组系统，机组系统ID为[%s]", ids));
        return JsonResult.ok(row);
    }

    /**
     * 导入机组系统及设备
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/import")
    public JsonResult importMachineSystem(@RequestParam MultipartFile file) throws Exception {
        machineSystemService.importMachineSystem(file, getUserId());
        return JsonResult.ok();
    }

    /**
     * 导出机组系统及设备
     * @param response
     * @return
     */
    @GetMapping("/export")
    public JsonResult exportMachineSystem(HttpServletResponse response){
        machineSystemService.exportMachineSystem(response);
        return JsonResult.ok();
    }

    /**
     * 获得由机组，系统，设备组成的树状图
     * @param machineNo
     * @return
     */
    @GetMapping("/getTree")
    public JsonResult getTree(@RequestParam String machineNo){
        return JsonResult.ok(machineSystemService.getMachineAndSystemTree(machineNo));
    }

}
